import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class CacultorL extends JFrame implements ActionListener{
    JPanel cards;
    CardLayout Clayout=new CardLayout();
    JPanel cardNorth;
    private final String keys[]={"%","√","x^2","1/x","CE","C","<-","/","7","8","9","*","4","5","6","-","1","2","3","+","+/-","0",".","="};
    private final String Rom[]={"MC","MR","M+","M-","MS","M"};
    private JButton Keys[]=new JButton[keys.length];
    private JButton rom[]=new JButton[Rom.length];
    static JMenuBar menuBar=new JMenuBar();
    JPanel CenterPanel=new JPanel();
    JPanel NorthPanel=new JPanel();
    JPanel SouthPanel=new JPanel();
    JTextField textField=new JTextField(17);
    JTextField textFieldUp=new JTextField(17);
    private double left;
    private double right;
    private static double result;
    private String Result;
    private String temp="";
    private static int option;
    private static String temp1="";
    private static int option1=0;
    private  static double right1;

    public static void main(String[] args) throws IOException {
        CacultorL window=new CacultorL();
        window.setSize(500,300);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.pack();

        window.setVisible(true);
        DataOutputStream dataOutputStream=new DataOutputStream(new FileOutputStream("src/a.dat"));
        dataOutputStream.writeDouble(result);
    }
    public CacultorL()  {
//       File Datap=new File("DataIn.txt");
//       FileWriter in=new FileWriter(Datap);

        JMenu fileItem=new JMenu("      功能菜单");
        JMenuItem card1=new JMenuItem("           科学");
        // JMenuItem card2=new JMenuItem("           标准");
        menuBar.add(fileItem);
        fileItem.add(card1);
        // fileItem.add(card2);
        cards=new JPanel();
        cards.setLayout(Clayout);
        cardNorth=new JPanel();
        cardNorth.setLayout(Clayout);
        SouthPanel.setLayout(new GridLayout(2,1));
        CenterPanel.setLayout(new GridLayout(6,4));
        NorthPanel.setLayout(new GridLayout(1,6));
        CenterPanel.setPreferredSize(new Dimension(250,150));
        NorthPanel.setPreferredSize(new Dimension(350,70));
        addRom(Rom);
        for(int i=0;i<keys.length;i++) {
            addKeys(keys[i],i);
        }
        SouthPanel.add(textFieldUp);
        SouthPanel.add(textField);
        add(SouthPanel,BorderLayout.NORTH);
        Kcacultor a=new Kcacultor();
//       add(a.center1,BorderLayout.SOUTH);
        add(NorthPanel,BorderLayout.CENTER);
        cards.add(CenterPanel);
        cards.add(a.center1);
        cardNorth.add(SouthPanel);
        cardNorth.add(a.NorthPanel);
        //Kcacultor.center1.setBackground(Color.BLACK);
        add(BorderLayout.SOUTH,cards);
        add(BorderLayout.NORTH,cardNorth);
        textField.setHorizontalAlignment(JTextField.RIGHT);
        textField.setEditable(false);
        textFieldUp.setHorizontalAlignment(JTextField.RIGHT);
        textFieldUp.setEditable(false);
        card1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Clayout.next(cards);
                Clayout.next(cardNorth);
                textField.setText("");
                textFieldUp.setText("");
                temp="";
                temp1="";
                left=0;
                right=0;
            }
        });

    }
    public void addKeys(String keys,int i){
        Keys[i]= new  JButton(keys);
        CenterPanel.add(Keys[i]);
        Keys[i].addActionListener(this);
    }
    public void addRom(String Rom[]){
        for(int i=0;i<Rom.length;i++){
            rom[i]= new  JButton(Rom[i]);
            NorthPanel.add(rom[i]);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==Keys[8]){
            temp+="7";
            temp1+="7";
            textField.setText(temp);

        }else if(e.getSource()==Keys[9]){
            temp1+="8";
            temp+="8";
            textField.setText(temp);

        }else if(e.getSource()==Keys[10]){
            temp1+="9";
            temp+="9";
            textField.setText(temp);

        }else if(e.getSource()==Keys[12]){
            temp1+="4";
            temp+="4";
            textField.setText(temp);

        }else if(e.getSource()==Keys[13]){
            temp1+="5";
            temp+="5";
            textField.setText(temp);

        }else if(e.getSource()==Keys[14]){
            temp1+="6";
            temp+="6";
            textField.setText(temp);

        }else if(e.getSource()==Keys[16]){
            temp1+="1";
            temp+="1";
            textField.setText(temp);
        }
        else if(e.getSource()==Keys[17]){
            temp1+="2";
            temp+="2";
            textField.setText(temp);
        }
        else if(e.getSource()==Keys[18]){
            temp1+="3";
            temp+="3";
            textField.setText(temp);
        }else if(e.getSource()==Keys[21]){
            temp1+="0";
            temp+="0";
            textField.setText(temp);

        }
        if(e.getSource()==Keys[0]){
            left=Double.parseDouble(textField.getText());
            textField.setText("");
            temp="";
            temp1+="%";
            textFieldUp.setText(temp1);
            option=2;
            option1=0;

        }else if(e.getSource()==Keys[23]){
            option1++;
            if(option==1){
                String Right=textField.getText();
                right=Double.parseDouble(Right);
                result=left+right;
                Result=""+result;
                if(option1>1){
                    result=left+right1;
                }
                Result=""+result;
                left=result;
            }else if (option==2){
                right=Double.parseDouble(textField.getText());
                result=left%right;
                Result=""+result;
            }else if(option==3){
                if(option1==1) {
                    right = Double.parseDouble(textField.getText());
                    result = left / right;
                    right1=right;
                }
                if(option1>1){
                    result=left/right1;
                }
                Result=""+result;
                left=result;
            }else if(option==4){
                right=Double.parseDouble(textField.getText());
                result=left*right;
                Result=""+result;
                if(option1>1){
                    result=left*right1;
                }
                Result=""+result;
                left=result;

            }else if(option==5){
                if(option1==1) {
                    right = Double.parseDouble(textField.getText());
                    result = left - right;
                    right1=right;
                }
                if(option1>1){
                    result=left-right1;
                }
                Result=""+result;
                left=result;



            }

            try {
                DataOutputStream   dataOutputStream = new DataOutputStream(new FileOutputStream("src/a.dat"));
                dataOutputStream.writeDouble(result);
                //byte[] buffer = new byte[1024];
                //dataOutputStream.write(buffer,0,8);
                dataOutputStream.close();

            } catch (Exception e1) {
                e1.printStackTrace();
            }
            textField.setText(Result);
        }else if (e.getSource()==Keys[1]){
            left=Double.parseDouble(textField.getText());
            textField.setText("");
            temp="";
            result=Math.sqrt(left);
            Result=""+result;
            temp1="√("+temp1+"）";
            textField.setText(Result);
            textFieldUp.setText(temp1);
        }else if(e.getSource()==Keys[2]){
            left=Double.parseDouble(textField.getText());
            textField.setText("");
            temp="";
            result=left*left;
            temp1="sqr("+temp1+"）";
            Result=""+result;
            textField.setText(Result);
            textFieldUp.setText(temp1);
            left=result;
        }else if(e.getSource()==Keys[3]){
            left=Double.parseDouble(textField.getText());
            textField.setText("");
            temp="";
            result=1/left;
            Result=""+result;
            textFieldUp.setText(Result);
        }else if(e.getSource()==Keys[4]){
            textField.setText("");
            temp="";
            temp1="";
            left=0;
            right=0;
        }else if(e.getSource()==Keys[19]){
            left=Double.parseDouble(textField.getText());
            temp1+="+";
            textFieldUp.setText(temp1);
            textField.setText("");
            temp="";
            option=1;
            option1=0;

        }else if(e.getSource()==Keys[5]){
            textField.setText("");
            textFieldUp.setText("");
            temp="";
            temp1="";

        }else if(e.getSource()==Keys[7]){
            left=Double.parseDouble(textField.getText());
            temp1+="/";
            textFieldUp.setText(temp1);
            textField.setText("");
            temp="";
            option=3;
            option1=0;

        }else if(e.getSource()==Keys[11]){
            left=Double.parseDouble(textField.getText());
            temp1+="*";
            textFieldUp.setText(temp1);
            textField.setText("");
            temp="";
            option=4;
            option1=0;

        }else if(e.getSource()==Keys[15]){
            left=Double.parseDouble(textField.getText());
            temp1+="-";
            textFieldUp.setText(temp1);
            textField.setText("");
            temp="";
            option=5;
            option1=0;

        }else if(e.getSource()==Keys[22]){
            temp1+=".";
            temp+=".";
            textField.setText(temp);
        }else if(e.getSource()==Keys[6]){
            temp1=textField.getText();
            temp1=temp1.substring(0,temp1.length()-1);
            temp=temp1;
            textField.setText(temp1);
        }else if(e.getSource()==Keys[20]){
            right=Double.parseDouble(textField.getText());
            right=-right;
            Result=""+right;
            textField.setText(Result);
            temp1=temp1.substring(0,temp1.length()-2);
            temp1+=textField.getText();

        }

    }

}